<?php
/**
 * 发放卡券（发送卡券消息）
 */
require_once ('common.php');
//发送消息地址
$sendmsg_url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=';


//获取access_token
$access_token = get_access_token();
//用户的openid
$openid  = trim($_GET['openid']);
$card_id = trim($_GET['card_id']);
if(empty($openid)){
    echo_Json(-1, 'openid不能为空');
}
if(empty($card_id)){
    echo_Json(-1, '卡券card_id不能为空');
}
//$openid = 'oEZwf1IRZc5a9EK-qK7kkUJLe5dw'; //wkk
//$openid = 'oEZwf1K3_44Mx6TMVDYf6ModD6xs'; //trm
//$card_id = 'pEZwf1DduS5dwTDyQXyqOp7B5xS4'; //折扣券
//$card_id = 'pEZwf1NqhsNipY9QMvwzIoqWC1Xg'; //团购券
/*---------------发送卡券消息 start--------------*/
$postcardData = array(
    'touser'  => $openid,
    'msgtype' => 'wxcard',
    'wxcard'  => array(
        'card_id'=> $card_id
    )
);
$isSendcardMsg = sendcardmsg($sendmsg_url.$access_token, $postcardData);
if($isSendcardMsg['code'] == 0){
    echo_Json(0, '卡券消息发送成功');
}else{
    echo_Json($isSendcardMsg['code'], '卡券消息发送失败');
}
/*---------------发送卡券消息 end--------------*/
//发送卡券消息
function sendcardmsg($url, $data){
    $msg_Arr = jsonpostJson($url, $data, true); //post请求
    if($msg_Arr['errcode'] != 0){
        $result = array(
            'code' => $msg_Arr['errcode'],
        );
    }else{
        $result = array(
            'code' => 0
        );
    }
    return $result;
}